Skip to content

Lib: add Broadcast Channel and Shared Worker bindings#2400

Open
hhugo wants to merge 5 commits into
masterfrom
broadcast-shared-worker
Open

Lib: add Broadcast Channel and Shared Worker bindings#2400
hhugo wants to merge 5 commits into
masterfrom
broadcast-shared-worker

Conversation

@hhugo

@hhugo hhugo commented Jul 11, 2026

Copy link
Copy Markdown
Member

Add two new modules to js_of_ocaml, filling the two remaining Tier 2 communication gaps from API-STATUS.md:

  • BroadcastChannel: the BroadcastChannel constructor, name, polymorphic postMessage, close, onmessage/onmessageerror (typed with the shared Dom_html.messageEvent), and an is_supported probe.
  • SharedWorker:
    • page side: sharedWorker (its port : MessageChannel.messagePort, onerror reusing Worker.errorEvent) with plain, named and options constructors (workerOptions: name/type/credentials);
    • worker side: sharedWorkerGlobalScope (name, close, onconnect), global (), and a set_onconnect helper that extracts the connecting MessagePort from the connect event, mirroring Worker.set_onmessage (including the Invalid_argument guard outside a shared worker scope).

Runtime tests exercise BroadcastChannel under Node (construction, name, instanceof, postMessage/close), gated off QuickJS; SharedWorker is browser-only so its tests cover the environment-independent parts (options record, support probe, the set_onconnect guard). CHANGES.md and API-STATUS.md are updated.

🤖 Generated with Claude Code

hhugo and others added 5 commits July 11, 2026 16:02
Add two new modules to js_of_ocaml:

- BroadcastChannel: the BroadcastChannel constructor, name, polymorphic
  postMessage, close, onmessage/onmessageerror and a support probe.
- SharedWorker: the page-side sharedWorker (port, onerror) with plain,
  named and options constructors (workerOptions: name/type/credentials),
  and the worker-side sharedWorkerGlobalScope (name, close, onconnect)
  with a set_onconnect helper that hands the connecting MessagePort to
  the callback.

Register the modules in js_of_ocaml.ml, add runtime tests (BroadcastChannel
gated off QuickJS; SharedWorker limited to the environment-independent
parts since the global is browser-only), and update CHANGES.md and
API-STATUS.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
A shared worker receives connect events as soon as the loader script has
been evaluated, while the wasm module is still being instantiated
asynchronously: nothing listens to these events yet and the connecting
page never gets its port. Capture connect events during start-up and
dispatch them again once the program is initialized.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
A page (lib/tests-browser/test_shared_worker.html) to open in several
tabs: every tab talks to a single shared worker that numbers connections
and echoes messages with a shared counter, the worker announces new
connections on a broadcast channel, and each tab can broadcast to the
others. Works in both js and wasm modes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Fixes the api-check test.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Served without a charset in the Content-Type header, browsers fall back
to windows-1252 and the em dash in the mode-switch line renders as
mojibake.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant